//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

using $prjsn$.Entity;
using $prjsn$.Common;
using $prjsn$.Common.Data;

namespace $safeprojectname$
{
    public class CustomListFilterValidator : FilterValidatorBase<CustomList, CustomListFilter, int>
    {
        protected override void Init(RuleCollection<CustomListFilter> rules)
        {
            base.Init(rules);
        }
    }

    public class CustomListValidator : CodeListValidator<CustomList, ICustomListDao>
    {
        private static readonly IList<string> _ignoredProperties = new List<string> { };

        protected override void Init(RuleCollection<CustomList> rules)
        {
            base.Init(rules);

            // string length
            rules.For(entity => entity.Code).RequireMaxLength(50);
            rules.For(entity => entity.Code).RequireLengthBetween(1, 2000).BreakOnError();

            // CustomType
            rules.Require(entity => entity.CustomListType);

            // parent is of the same CustomListType
            rules.AssureThatReference(entity => entity.Parent)
                 .HasSetting((entity, parent) => entity.CustomListType.Equals(parent.CustomListType));

            // unique
            rules.IsUnique(entity => Dao.Find(entity.ID, new CustomListFilter
            {
                CodeExact = entity.Code,
                CustomListType = entity.CustomListType,
                ParentId = new[] { entity.Parent.Is() ? entity.ParentId : null },
            }), "Code", "CustomListType", "Parent");

            // TODO
            //rules.Evaluate(IsCustomListInUse).WhenDeleting();
        }

        #region MetaData

        protected override IList<string> AllwaysIgnoredProperties
        {
            get { return _ignoredProperties.Append(base.AllwaysIgnoredProperties); }
        }

        #endregion MetaData
    }
}